﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;

public partial class ProfileOrderControl : System.Web.UI.UserControl
{
    string connectionString;
    private SqlConnection con;
    private SqlDataAdapter da;
    private SqlCommand cmd;
    private DataSet ds;

    int cusID = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        connectionString = WebConfigurationManager.ConnectionStrings["myCon"].ToString();
        con = new SqlConnection(connectionString);
        con.Open();
        getCusID(Session["User"].ToString());
        if (!IsPostBack)
        {
            bindDataToGridViewOrder(cusID);
        }
    }
    protected void getCusID(string userName)
    {
        try
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            ds = new DataSet();
            cmd = new SqlCommand("spGetAccount", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@userName", SqlDbType.VarChar).Value = userName;
            da = new SqlDataAdapter(cmd);
            da.Fill(ds, "Account");
            cusID = Int32.Parse(ds.Tables["Account"].Rows[0][0].ToString());

        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            con.Close();
        }
    }

    protected void bindDataToGridViewOrder(int CusID)
    {
        try
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            ds = new DataSet();
            cmd = new SqlCommand("spGetOrderPendingDeclined", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@CusID", SqlDbType.Int).Value = CusID;
            da = new SqlDataAdapter(cmd);
            da.Fill(ds, "OrderPendingDeclined");
            gridViewOrder.DataSource = ds.Tables["OrderPendingDeclined"];
            gridViewOrder.DataBind();
            
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            con.Close();
        }
    }

    protected void btnYourProfile_Click(object sender, EventArgs e)
    {
        pnProfiles.Visible = true;
        pnOrder.Visible = false;
    }
    protected void btnYourOrder_Click(object sender, EventArgs e)
    {
        pnProfiles.Visible = false;
        pnOrder.Visible = true;
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        txtOldPass.Text = "";
        txtNewPass.Text = "";
        txtConNewPass.Text = "";
    }

    protected void checkPass(string user, string pass, string newpass)
    {
        try
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            ds = new DataSet();
            cmd = new SqlCommand("spGetAccount", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@userName", SqlDbType.VarChar).Value = user;
            cmd.ExecuteNonQuery();
            da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            if (ds.Tables[0].Rows[0][2].ToString() != pass)
            {
                lblStatusChangePass.Text = "Your Old Pass is not correct";
            }
            else if (ds.Tables[0].Rows[0][2].ToString() == pass)
            {
                UpdatePass(user, newpass);
                lblStatusChangePass.Text = "Change Password Successfull";
            }
            
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            con.Close();
        }
    }

    protected void UpdatePass(string user, string newpass)
    {
        try
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            cmd = new SqlCommand("spUpdateCustomer", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@UserName", SqlDbType.VarChar).Value = user;
            cmd.Parameters.Add("@Password", SqlDbType.VarChar).Value = newpass;
            cmd.ExecuteNonQuery();   

        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            con.Close();
        }
    }

    protected void btnChangePass_Click(object sender, EventArgs e)
    {
        checkPass(Session["User"].ToString(), txtOldPass.Text, txtNewPass.Text);
    }
}